<!DOCTYPE html>
<html>
<head>

    <title>RGraph demo: A basic example of dynamics updates with AJAX</title>
    
    <meta name="robots" content="noindex,nofollow" />
    <meta name="description" content="A basic example of a chart created using dynamic updates and AJAX" />
    <meta name="googlebot" content="NOODP">
    
    <link rel="stylesheet" href="demos.css" type="text/css" media="screen" />

    <!-- Include the RGraph libraries -->
    <script src="../libraries/RGraph.common.core.js" ></script>
    <script src="../libraries/RGraph.line.js" ></script>

</head>

<body>

    <!-- Share buttons -->
    <p style="float: right">
        <script>
            document.write('<a href="" target="_blank" onclick="window.open(\'https://www.facebook.com/sharer/sharer.php?u=http://www.rgraph.net' + location.pathname + '\', null, \'top=50,left=50,width=600,height=368\'); return false"><img src="../images/facebook-large.png" width="200" height="43" alt="Share on Facebook" border="0" title="Visit the RGraph Facebook page" id="facebook_link" /></a>&nbsp;');
            document.write('<a href="https://twitter.com/_rgraph" target="_blank" onclick="window.open(\'https://twitter.com/intent/tweet?text=Check%20out%20this%20demo%20of%20RGraph:%202D/3D%20JavaScript%20charts%20-%20Free%20and%20Open%20Source%20http://www.rgraph.net' + location.pathname + '\', null, \'top=50,left=50,width=700,height=400\'); return false"><img src="../images/twitter-large.png" width="200" height="43" alt="Share on Twitter" border="0" title="Mention RGraph on Twitter" id="twitter_link" /></a>');
        </script>
    </p>


    <h1>A basic example of a chart created using dynamic updates and AJAX</h1>
    
    <p>
        This is a basic example of dynamic updates using AJAX to request the data from the server. There are more examples here:
    </p>
    
    <ul>
        <li><a href="/demos/line-dynamic-updates-range.html">/demos/line-dynamic-updates-range.html</a></li>
        <li><a href="/demos/line-dynamic-updates.html">/demos/line-dynamic-updates.html</a></li>
    </ul>

    <canvas id="cvs" width="600" height="250">[No canvas support]</canvas>

    <script>
    
        // Prefill the data array
        for (i=0,data=[];i<60; ++i) data[i] = null;
    
        /**
        * Ths window.onload function initiates the AJAX request. The AJAX page is: http://www.rgraph.net/getdata.html
        * If you view this in your browser you'll see that all it does is output a sequence of numbers.
        */
        window.onload = function ()
        {
            var line = new RGraph.Line({
                id: 'cvs',
                data: data,
                options: {
                    labels: ['60s','55s','50s','45s','40s','35s','30s','25s','20s','15s','10s','5s','0s'],
                    numxticks: 12,
                    backgroundGridAutofitNumvlines: 12,
                    ymax: 100,
                    gutterLeft: 35,
                    textAccessible: true
                }
            }).draw();


            /**
            * This is the AJAX callback function. It adds the number retrieved via
            * AJAX to the data array
            */
            function draw ()
            {
                // Set the data on the object
                line.original_data[0].push(RGraph.random(0, 100));
                line.original_data[0].shift();

                // Clear the canvas
                RGraph.clear(line.canvas);
                line.draw();

                setTimeout(draw, 1000);
            }
        
        
            
            setTimeout(draw, 1000);
        };
    </script>







    <p></p>

    This goes in the documents header:
    <pre class="code">
&lt;script src="RGraph.common.core.js"&gt;&lt;/script&gt;
&lt;script src="RGraph.line.js"&gt;&lt;/script&gt;
</pre>
    
    Put this where you want the chart to show up:
    <pre class="code">
&lt;canvas id="cvs" width="600" height="250"&gt;
    [No canvas support]
&lt;/canvas&gt;
</pre>

    This is the code that generates the chart:
    <pre class="code">
&lt;script&gt;    
    
    <span>// Prefill the data array</span>
    for (i=0,data=[];i&lt;60; ++i) data[i] = null;

    <span>/**
    * Ths window.onload function initiates the AJAX request. The AJAX page is: http://www.rgraph.net/getdata.html
    * If you view this in your browser you'll see that all it does is output a sequence of numbers.
    */</span>
    window.onload = function ()
    {
        var line = new RGraph.Line({
            id: 'cvs',
            data: data,
            options: {
                labels: ['60s','55s','50s','45s','40s','35s','30s','25s','20s','15s','10s','5s','0s'],
                numxticks: 12,
                backgroundGridAutofitNumvlines: 12,
                ymax: 100,
                gutterLeft: 35,
                textAccessible: true
            }
        }).draw();


        <span>/**
        * This is the AJAX callback function. It adds the number retrieved via
        * AJAX to the data array
        */</span>
        function draw ()
        {
            <span>// Set the data on the object</span>
            line.original_data[0].push(RGraph.random(0, 100));
            line.original_data[0].shift();

            <span>// Clear the canvas</span>
            RGraph.clear(line.canvas);
            line.draw();

            setTimeout(draw, 1000);
        }
    
    
        
        setTimeout(draw, 1000);
    };
&lt;/script&gt;
</pre>


    
</body>
</html>